package py.com.fms.session;

import java.io.*;
import java.security.*;

import org.jboss.util.Base64;

import py.com.fms.exception.CommonException;


public final class PasswordServices {
	
	public synchronized String encryptPass(String pass) throws CommonException {
		
		MessageDigest md = null;
		
		try {
			
			// Get instance of java security digest message using SHA algorithm	
			md = MessageDigest.getInstance("SHA");
										
			
		}catch (NoSuchAlgorithmException e) {
			
			throw new CommonException(e.getMessage());
			
		}
		
		try {
			
			// Covert the string into a byte-representation using UTF-8 encoding format
			md.update(pass.getBytes("UTF-8"));
			
		}catch (UnsupportedEncodingException e) {
			
			throw new CommonException(e.getMessage());
		}
		// Generate an array of bytes that represent the digested (encrypted) password 
		byte raw[] = md.digest();
		
		// Create a String representation of the byte array representing the digested password
		String hash = Base64.encodeBytes(raw);
		
		// Returning the generated hash
		return hash;
	}


}
